package cn.edu.xjtu.competiton.力扣.双周赛.biweekly77;

/**
 * @author Hydrion-QLz
 * @date 2022-04-30 23:30
 * @description https://leetcode-cn.com/contest/biweekly-contest-77/problems/count-unguarded-cells-in-the-grid/
 */
public class Third {
    public int countUnguarded(int m, int n, int[][] guards, int[][] walls) {
        int[][] visited = new int[m][n]; // 0空格，1墙，2守卫,3看过
        int ans = 0;
        for (int[] point : walls) {
            visited[point[0]][point[1]] = 1;
        }
        for (int[] point : guards) {
            visited[point[0]][point[1]] = 1;
        }

        for (int i = 0; i < guards.length; i++) {
            int row = guards[i][0];
            int col = guards[i][1];
            // 右边
            for (int j = col + 1; j < n; j++) {
                if (visited[row][j] == 1 || visited[row][j] == 2) {
                    break;
                }
                if (visited[row][j] == 0) ans++;
                visited[row][j] = 3;
            }

            // 左边
            for (int j = col - 1; j >= 0; j--) {
                if (visited[row][j] == 1 || visited[row][j] == 2) {
                    break;
                }
                if (visited[row][j] == 0) ans++;
                visited[row][j] = 3;
            }

            // 上面
            for (int j = row - 1; j >= 0; j--) {
                if (visited[j][col] == 1 || visited[j][col] == 2) {
                    break;
                }
                if (visited[j][col] == 0) ans++;
                visited[j][col] = 3;
            }

            // 下面
            for (int j = row + 1; j < m; j++) {
                if (visited[j][col] == 1 || visited[j][col] == 2) {
                    break;
                }
                if (visited[j][col] == 0) ans++;
                visited[j][col] = 3;
            }
        }
        return m * n - ans - guards.length - walls.length;
    }
}
